﻿var ajax = {
    get: function (url, callback) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                callback && callback(xhr);
            }
        };

        xhr.send();
    }
};
var ui = {
    tab: {
        items: [],
        click: function (sender,idx) {
            var el_pages = document.getElementById('_div_pages');
            var item = ui.tab.items[idx];
            if (item.page) {
                ajax.get(item.page, function (xhr) {
                    el_pages.innerHTML = xhr.responseText;
                })
            } else {
                el_pages.innerHTML = '页面未配置，请修改project.json中的tabs项' + item.text + '的page';
            }

            if (item.color_selected) {
                sender.style.color = item.color_selected;
            }
        }
    }
};
var agileui = {
    init: function () {
        ajax.get("project.json", function (xhr) {
            var json = xhr.responseText;
            var config = JSON.parse(json);

            var el_pages = document.getElementById('_div_pages');
            var el_tabs = document.getElementById('_div_tabs');
            var ht = document.documentElement.clientHeight;

            if (config && config.tabs && config.tabs.length > 0) {
                ui.tab.items = config.tabs;
                var sb = '';
                for (var i = 0; i < config.tabs.length; i++) {
                    var item = config.tabs[i];
                    var color = 'color:#888;';
                    if (item.color) {
                        color = 'color:' + item.color;
                    }
                    sb += '<div style="flex:1;padding:10px;' + color + '" onclick="ui.tab.click(this,' + i + ')">';
                    sb += '<div style="text-align:center;"><img style="height:24px;width:24px;" src="' + item.icon + '" /></div>';
                    sb += '<div style="text-align:center;">' + item.text + '</div>';
                    sb += '</div>';
                }
                el_tabs.style.display = 'flex';
                el_tabs.style.height = '60px';
                el_tabs.style.top = (ht - 60) + 'px';
                el_tabs.style.backgroundColor = '#ededed';
                el_tabs.innerHTML = sb;
                el_pages.style.height = (ht - 60) + 'px';
            }

            if (config.index) {
                ajax.get(config.index, function (xhr2) {
                    el_pages.innerHTML = xhr2.responseText;
                })
            } else {
                el_pages.innerHTML = '首页未配置，请修改project.json中的index参数';
            }
        })
    }
};